package com.gxuwz.lhleavesys.service.impl;

import com.gxuwz.lhleavesys.dao.StudentDao;
import com.gxuwz.lhleavesys.dao.UserDao;
import com.gxuwz.lhleavesys.entity.User;
import com.gxuwz.lhleavesys.service.StudentService;
import com.gxuwz.lhleavesys.service.TeacherService;
import com.gxuwz.lhleavesys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    public boolean registerUser(User user) throws Exception {
        return true;
    }

    public void disableUser(String userid) throws Exception {
        User user = new User();
        user.setUserid(userid);
        userDao.update(user);
    }

    @Override
    public boolean loginCheck(String username, String password, String role) throws SQLException {
        boolean loginSuccess = false;
        if (role.equals("system")) {
            loginSuccess = checkUser(username, password);
        }
        else if (role.equals("teacher")) {
            loginSuccess = teacherService.checkUser(username, password);
        }
        else if (role.equals("student")) {
            loginSuccess = studentService.checkUser(username, password);
        }
        return loginSuccess;
    }

    @Override
    public void addUser(User user, String role) {

    }

    ;

    /**
     * 添加用户
     * 
     * @param user
     * @throws Exception
     */
    public void addUser(User user) throws Exception {
        userDao.insert(user);
    }

    /**
     * 修改用户
     * 
     * @param user
     * @throws Exception
     */
    public boolean editUser(User user) throws Exception {
        return userDao.update(user);
    }

    /**
     * 删除用户
     * 
     * @param userid
     * @throws Exception
     */
    public void delUser(String userid) throws Exception {
        userDao.delete(userid);
    }

    public boolean checkUser(String userid, String pwd) throws SQLException {
        User user = new User();
        user.setPassword(pwd == null ? "0" : pwd);
        user.setUserid(userid == null ? "@" : userid);
        List<User> list = userDao.query(user);
        return !list.isEmpty();
    }

    @Override
    public User user() throws SQLException {
        return userDao.queryUser();
    }

    public List<User> listUser() {
        return new ArrayList<User>();
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public User findByName(String name) {
        return null;
    }

    @Override
    public User findByID(String id) {
        return null;
    }

    @Override
    public List<User> getUserList() {
        return null;
    }
}
